<template>
  <base-breadcrumb class="ModelList-container">
    <a-card>
      <div class="top-wrapper">
        <div class="project-info-box">
          <span>项目名称：</span>
          <span class="info-strong">{{ form.projectName || '无' }}</span>
        </div>
        <a-button
          :loading="projectLoading"
          :icon="form.projectName ? 'swap' : 'reload'"
          v-if="$currentPost().code !== 'PD'"
          type="primary"
          class="ml12"
          @click="form.projectName ? (visible = true) : getProject()"
        >
          {{ projectLoading ? '加载项目中' : form.projectName ? '切换项目' : '重新加载项目' }}
        </a-button>
      </div>
      <a-tabs v-model="activeKey" :animated="false">
        <a-tab-pane key="list" tab="模型列表">
          <ListComponent :projectInfo="form" :activeKey.sync="activeKey" />
        </a-tab-pane>
        <a-tab-pane key="compose" tab="组合模型">
          <ComposeComponent :projectInfo="form" />
        </a-tab-pane>
      </a-tabs>
    </a-card>
    <DialogForm
      ref="dialogForm"
      :visible.sync="visible"
      edit-model="E"
      :form="form"
      header-title="切换项目"
      :width="700"
      :isEmit="false"
      :rules="rules"
      :form-list="formList"
      @confirm="submitForm"
      :clearForm="false"
      :styles="{ width: '450px' }"
    />
  </base-breadcrumb>
</template>

<script>
import ListComponent from './ListComponent'
import ComposeComponent from './ComposeComponent'
import DialogForm from '@/components/DialogForm' // 弹窗
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目名称
import { getAuditProject } from '@/api/themeChoose/themeChoose'
import ModelDetail from '@/components/BimIntegrate' // BIM模型
import { validateToken } from '@/api/bimIntegrate/request'

export default {
  name: 'a' + Date.now(),
  components: {
    ListComponent,
    ComposeComponent,
    ModelDetail,
    DialogForm
  },
  data() {
    return {
      activeKey: 'list',
      visible: false, // 弹窗控件
      projectLoading: false, // 项目加载控件
      form: {
        projectName: '',
        projectId: '',
        branchCompanyId: ''
      }, // 表单
      rules: {
        branchCompanyId: [{ required: true, message: '请选择分公司', trigger: 'change' }],
        projectId: [{ required: true, message: '请选择项目', trigger: 'change' }]
      }
    }
  },
  computed: {
    formList() {
      return [
        {
          type: SelectSubCompany,
          keyName: 'branchCompanyId',
          label: '分公司',
          span: 24,
          attrs: {
            autoSelected: this.$currentPost().code === 'CO'
          }
        },
        {
          type: SelectProject,
          keyName: 'projectId',
          label: '项目',
          span: 24,
          attrs: {
            autoSelected: this.$currentPost().code === 'CO',
            keyValue: 'branchCompanyId',
            isEdit: true,
            form: this.form
          },
          selectChange: (e) => {
            this.form.projectInfo = e
          }
        }
      ]
    }
  },
  beforeRouteLeave(to, from, next) {
    const previewPath = '/bimIntegrate/model/modelPreview'
    if (to.path === previewPath) {
      sessionStorage.setItem('modelListForm', JSON.stringify(this.form))
    } else {
      sessionStorage.removeItem('modelListForm')
    }
    next()
  },
  created() {
    try {
      if(!sessionStorage.getItem("bimToken")) {
        validateToken()
      }
      const formStr = sessionStorage.getItem('modelListForm')
      if(!formStr) throw new Error('modelListForm Error')
      this.form = JSON.parse(formStr) || {}
    } catch {
      // 获取项目
      this.getProject()
    }
  },
  methods: {
    /**
     * 获取项目
     */
    getProject() {
      const postId = this.$currentPost().id
      const postCode = this.$currentPost().code
      const params = { PD: { deptOrgIds: [postId] }, BC: { branchComIds: [postId] }, CO: { branchComIds: [] } }[
        postCode
      ] // 根据当前组织层级获取当前项目

      // 设置分公司
      postCode === 'BC' && (this.form.branchCompanyId = postId)

      // 设置默认项目
      this.projectLoading = true
      getAuditProject(params)
        .then((res) => {
          this.projectLoading = false
          if (res.code === 200) {
            this.form.projectName = res.data[0]?.projectName
            this.form.projectId = res.data[0]?.projectId
          }
        })
        .catch(() => {
          this.projectLoading = false
        })
    },
    /**
     * 选择项目
     */
    submitForm(e) {
      this.form = Object.assign({}, e, { projectName: this.form.projectInfo?.name })
      this.visible = false
    }
  }
}
</script>

<style lang="less" scoped>
.top-wrapper {
  display: flex;
  align-items: center;
  .project-info-box {
    font-size: 16px;
    .info-strong {
      font-weight: bolder;
    }
  }
}
</style>
